﻿Imports System.Data.SqlClient
Public Class frmHoaDon
    Dim cnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=QLKS;Integrated Security=True"
    Dim con As New SqlConnection(cnStr)
    Private Sub frmHoaDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadCBXPhieuThue()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub loadCBXPhieuThue()
        Dim sql As String = "select MaPhieu,TenPhong from PhieuThue"
        Dim dt As New DataTable()
        Dim da As New SqlDataAdapter(sql, con)
        da.Fill(dt)
        Dim bs As New BindingSource()
        bs.DataSource = dt
        cbxPhieuThue.DataSource = bs
        cbxPhieuThue.DisplayMember = "TenPhong"
        cbxPhieuThue.ValueMember = "TenPhong"
    End Sub

    Private Sub cbxPhieuThue_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxPhieuThue.SelectedIndexChanged
        Dim sql As String = "select * from PhieuThue,Phong,LoaiPhong,LoaiKhach where LoaiKhach.MaLoaiKhach=PhieuThue.MaLoaiKhach   and LoaiPhong.MaLoaiPhong=Phong.MaLoaiPhong and PhieuThue.TenPhong=Phong.TenPhong and PhieuThue.TenPhong='" + cbxPhieuThue.SelectedValue.ToString() + "'"
        Dim da As New SqlDataAdapter(sql, con)
        'Dim dtset As New DataSet()
        'da.Fill(dtset, "PhieuThue")
        'LbTenK.DataBindings.Add("text", dtset, "PhieuThue.TenKhach")
        Dim dt As New DataTable()
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            LbTenK.Text = dt.Rows(0)("TenKhach").ToString()
            lbDiaChi.Text = dt.Rows(0)("DiaChi").ToString()
            lbTenP.Text = dt.Rows(0)("TenPhong").ToString()
            LbNgayBatDau.Text = dt.Rows(0)("NgayBatDau")
            lbLoaiK.Text = dt.Rows(0)("TenLoaiKhach").ToString()

            Dim ngaybatdau As DateTime = dt.Rows(0)("NgayBatDau")
            Dim ngayketthuc As DateTime = DateTimePicker1.Value.Date
            Dim songay As Integer
            songay = ngayketthuc.ToOADate - ngaybatdau.ToOADate
            LbSoNgay.Text = songay.ToString()

            LbLoaiPhong.Text = dt.Rows(0)("MaLoaiPhong").ToString()
            lbGiatien.Text = dt.Rows(0)("Gia").ToString()
            lbSoNg.Text = dt.Rows(0)("SoKhach").ToString()

            Dim songuoi As Integer = dt.Rows(0)("SoKhach").ToString()
            Dim gia As Integer = dt.Rows(0)("Gia").ToString()
            Dim tongtien As Integer = songay * gia
            
            If (songuoi < 3) Then
                tongtien = tongtien
            ElseIf songuoi > 2 And lbLoaiK.Text = "Nước ngoài" Then
                tongtien = (tongtien + (tongtien * ((songuoi - 2) * 0.25))) * 1.5
            End If
            If lbLoaiK.Text = "Nước ngoài" And songuoi < 3 Then
                tongtien = tongtien * 1.5
            End If
            LbTongTien.Text = tongtien & ".000"
        End If

    End Sub

    
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If (LbTenK.Text <> "?") Then
            If MessageBox.Show("Chắc chắn chưa?", "Thông Báo", MessageBoxButtons.OKCancel, _
                               Nothing, MessageBoxDefaultButton.Button1) = DialogResult.OK Then
                Dim sql As String = "insert into HoaDon(TenKhachHang,DiaChi,NgayThanhToan,ThanhTien,SoNgayThue,SoKhach,TenPhong) values(@TenKhachHang,@DiaChi,@NgayThanhToan,@ThanhTien,@SoNgayThue,@SoKhach,@TenPhong)" + "delete from PhieuThue where TenPhong='" + cbxPhieuThue.SelectedValue.ToString() + "'" + "update Phong set TrangThai=@TrangThai where TenPhong='" + cbxPhieuThue.SelectedValue.ToString() + "'"
                Dim command As New SqlCommand(sql, con)

                command.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 30)
                command.Parameters("@TenKhachHang").Value = LbTenK.Text

                command.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 30)
                command.Parameters("@DiaChi").Value = lbDiaChi.Text

                command.Parameters.Add("@NgayThanhToan", SqlDbType.Date, 30)
                command.Parameters("@NgayThanhToan").Value = DateTimePicker1.Value.Date

                command.Parameters.Add("@ThanhTien", SqlDbType.NVarChar, 30)
                command.Parameters("@ThanhTien").Value = LbTongTien.Text

                command.Parameters.Add("@SoNgayThue", SqlDbType.Int, 10)
                command.Parameters("@SoNgayThue").Value = LbSoNgay.Text

                command.Parameters.Add("@SoKhach", SqlDbType.Int, 10)
                command.Parameters("@SoKhach").Value = lbSoNg.Text

                command.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 30)
                command.Parameters("@TenPhong").Value = lbTenP.Text

                command.Parameters.Add("@TrangThai", SqlDbType.NVarChar, 10)
                command.Parameters("@TrangThai").Value = "Trống"

                command.Connection.Open()
                Dim i = command.ExecuteNonQuery()
                If (i > 0) Then
                    MessageBox.Show("Đã thêm hóa đơn")
                Else
                    MessageBox.Show("Xảy ra lỗi")
                End If
                command.Connection.Close()
            End If
            loadCBXPhieuThue()
        Else
            MessageBox.Show("Hãy chọn phiếu thuê!")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New FrmXemDS_HoaDon
        frm.ShowDialog()
    End Sub
End Class